#ifndef TRANSFORMERCONTENTRECORDMEMROYIN_HPP
#define TRANSFORMERCONTENTRECORDMEMROYIN_HPP
class TransContentRecordMemoryIn: public TransContentITimerEvent{
	private:
		int localPort;
		int mCapacity;
		int mInsertIndex;
		int mAvaliableIndex;
		int mAvaliableCount;
		int mRecordInnerOffset;
		int mRecordLeftBytes;
		int mDataAvaliableElapseTime;
		int timeout;
		short* recordBuffer;
		receiveFromSyncCallback rfscb;
		pthread_cond_t waitCond;
		pthread_mutex_t waitMutex;
		pthread_mutex_t mDataAvaliableMutex;
		pthread_cond_t mDataAvaliableCond;
		char* dataStoreBuffer;
		int dataStoreBufferLen;
		int readBytes = 0;

		pthread_t receivePid;
	public:
		void OnTime();
		TransStreamInMemory(): localPort(0), mCapacity(PACKET_BUFFER_SIZE_DEFAULT), mInsertIndex(0), mAvaliableIndex(0), mAvaliableCount(0), mRecordInnerOffset(0), mRecordLeftBytes(0), mDataAvaliableElapseTime(0), timeout(INT_MAX), recordBuffer(NULL), rfscb(NULL), waitCond(), waitMutex(), mDataAvaliableMutex(), mDataAvaliableCond(), dataStoreBuffer(NULL), dataStoreBufferLen(0), readBytes(0){
			debug_enter();
			recordBuffer = (short*)calloc(mCapacity, sizeof(short));
			for(int i=0; i< mCapacity; ++i){
				recordBuffer[i] = -1;
			}
			pthread_create(&receivePid, NULL, receiveRoute, this);
			pthread_detach(receivePid);
			debug_leave();
		}

		~TransStreamInMemory(){
			pthread_cancel(receivePid);
			if(recordBuffer != NULL){
				free(recordBuffer);
				recordBuffer = NULL;
			}
		}

		void setLocalPort(int port){
			this->localPort = port;
		}

		int getCapacity() const {
			return this->mCapacity;
		}

		int getTimeout() const {
			return this->timeout;
		}
		void setTimeout(int timeout){
			this->timeout = timeout;
		}

		int receiveFrom(int flag, void* buffer, int bufferLen, int timeout, receiveFromSyncCallback rfscb);
		
		int getData(char* buffer, int len);
		int response(char* content, int index);

	private:
		int writeData(char* content, int index);

		static void* finishedRoute(void* p){
			(TransStreamInMemory*(p))->finishCallback();
			return NULL;
		}
		void finishCallback(){
			this->rfscb(localPort)
		}

		void receiveFinished(){
			pthread_t pid;
			if(this->rfscb != NULL){
				pthread_create(&pid, NULL, finishedRoute, this);
				pthread_detach(pid);
				return;
			}
			pthread_mutex_lock(&waitMutex);
			pthread_cond_signal(&waitCond);
			pthread_mutex_unlock(&waitMutex);			
		}

		void newReceivedDataRequest();

		static void* receiveRoute(void* p);
};
#endif //TRANSFORMERCONTENTRECORDMEMROYIN_HPP
